/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.ui.preferences.formatter.tab;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.dltk.ui.formatter.FormatterModifyTabPage;
import org.eclipse.dltk.ui.formatter.IFormatterControlManager;
import org.eclipse.dltk.ui.formatter.IFormatterModifyDialog;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.php.internal.ui.PHPUiPluginExt;
import org.eclipse.php.internal.ui.preferences.formatter.PHPFormatterControlManager;
import org.eclipse.php.internal.ui.preferences.formatter.PHPPreview;
import org.eclipse.php.internal.ui.util.ReflectionUtils;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

/**
 * @author Sean W. Quinn (swquinn@gmail.com)
 */
abstract public class PHPCodeFormatterTabPage extends FormatterModifyTabPage {

	private static final int DEFAULT_COLUMN_SPAN = 4;

	/**
	 * Constant array for boolean selection
	 */
	protected static int COLUMN_SPAN = DEFAULT_COLUMN_SPAN;

	protected final IDialogSettings dialogSettings;
	protected final Map workingValues;
	protected PHPPreview preview;
	private Button showInvisibleButton;

	/**
	 * @param dialog
	 */
	public PHPCodeFormatterTabPage(final IFormatterModifyDialog dialog) {
		this(dialog, new HashMap());
	}

	/**
	 * @param dialog
	 */
	public PHPCodeFormatterTabPage(final IFormatterModifyDialog dialog, final Map workingValues) {
		super(dialog);
		this.dialogSettings = PHPUiPluginExt.getDefault().getDialogSettings();
		this.workingValues = workingValues;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void createOptions(final IFormatterControlManager manager, final Composite parent) {
		final PHPFormatterControlManager phpManager = PHPFormatterControlManager.newInstance(manager);
		doCreateOptions(phpManager, parent);
	}

	/**
	 * @return
	 */
	protected IFormatterModifyDialog getDialog() {
		final IFormatterModifyDialog dialog = ReflectionUtils.getFieldValue(this, "dialog");
		return dialog;
	}

	protected void doUpdatePreview() {
		boolean showInvisible= isShowInvisible();
		preview.showInvisibleCharacters(showInvisible);
		showInvisibleButton.setSelection(showInvisible);
	}

	private boolean isShowInvisible() {
		return dialogSettings.getBoolean(SHOW_INVISIBLE_PREFERENCE_KEY);
	}

	protected void notifyValuesModified() {
		// TODO: Implement modification listener...
	}

	/**
	 * @param manager
	 * @param parent
	 */
	abstract protected void doCreateOptions(final PHPFormatterControlManager manager, final Composite parent);
}
